<template>
  <div>
    <h2>登录</h2>
    <input type="text" v-model="user.customerName" placeholder="用户名"><br>
    <input type="password" v-model="user.loginPassword" placeholder="密码"><br>
    <input type="text" v-model="user.code" placeholder="验证码"><br>
    <img :src="captchaImage" @click="generateCaptcha" alt="验证码"><br>
    <button @click="login">登录</button><br>
    <el-link type="primary" @click="nowRegister">立即注册</el-link>&nbsp;&nbsp;<el-link type="primary" @click="nowPassword">找回密码</el-link><br>
  </div>
</template>

<script>


import {uuid} from "@/utils";

export default {
  data() {
    return {
      user:{},
      captchaImage: ''
    };
  },
  methods: {
    nowRegister() {
      this.$router.push("/Register")
    },
    nowPassword() {
      this.$router.push("/password")
    },
    generateCaptcha() {
      this.axios.get('/customer/CustomerGenerator',{params:{uid:uuid()}})
          .then(response => {
            this.captchaImage = response.data.data;
          })
          .catch(error => {
            console.error(error);
          });
    },
    login() {
      this.axios.post('/customer/login', this.user)
          .then(response => {
            console.log(response.data);
            if (response.data.code == 200) {
              this.$message.success(response.data.msg);
              // 处理登录成功的逻辑
              console.log(response.data.data);
              localStorage.setItem("user",this.user.customerName)
              localStorage.setItem("token",response.data.data)
              this.$router.push("customer");
            } else {
              this.$message.error(response.data.msg)
            }
          })
          .catch(error => {
            console.error(error.response.data);
          });
    }
  },created() {
    this.generateCaptcha();
  }
};
</script>

<style scoped>

</style>
